import { observable, action } from '@/miniprogram_npm/mobx-miniprogram/index';

const intervalStore = observable({
    interval: null,
    currentSecond: 0,

    setNewInterval: action(function (payload) {
        this.currentSecond = payload;
        this.interval = setInterval(() => {
            if (this.currentSecond === 0) {
                clearInterval(this.interval);
                this.interval = null;
                return;
            }
            this.currentSecond -= 1;
        }, 1000);
    }),

    clearInterval: action(function () {
        clearInterval(this.interval);
        this.interval = null;
        this.currentSecond = 0;
    })
});

export default intervalStore;
